package com.jumi.microservice.utils;

import com.jumi.microservice.common.enumeration.CommonResponseEnum;
import com.jumi.microservice.common.exception.BaseException;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.Security;

/**
 * @author Dirk
 * @Date 2020-08-26 17:45
 */
public class Sm3Util {
    static {
        Security.addProvider(new BouncyCastleProvider());
    }

    private Sm3Util() {
    }

    public static String sm3(String input, String key) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SM3", "BC");
            byte[] result = digest.digest(input.concat(key).getBytes(StandardCharsets.UTF_8));
            return Hex.toHexString(result);
        } catch (Exception e) {
            throw new BaseException(CommonResponseEnum.SERVER_ERROR);
        }
    }
}
